// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.dbmanager.ui;

import org.dengues.commons.DenguesCommonsPlugin;
import org.dengues.commons.ui.swt.colorstyledtext.ColorManager;
import org.dengues.commons.ui.swt.colorstyledtext.ColorStyledText;
import org.dengues.commons.ui.swt.colorstyledtext.EModeLanguage;
import org.dengues.commons.ui.swt.colorstyledtext.StyledTextContentAdapter;
import org.dengues.commons.ui.swt.proposal.SQLEditorProposalAdapter;
import org.dengues.core.metadata.DatabaseContainer;
import org.dengues.warehouse.dbmanager.ui.proposal.SQLEditorLabelProvider;
import org.dengues.warehouse.dbmanager.ui.proposal.SQLEditorProposalProvider;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.IContentProposalProvider;
import org.eclipse.jface.fieldassist.IControlContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class SqlEditorComposite extends AbstractEditorComposite {

    private DatabaseManagerDialog dialog;

    private StyledText colorText;

    private DatabaseContainer databaseContainer;

    /**
     * Qiang.Zhang.Adolf@gmail.com SqlEditorComposite constructor comment.
     * 
     * @param parent
     * @param style
     */
    public SqlEditorComposite(Composite parent) {
        super(parent, SWT.NONE);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.warehouse.dbmanager.ui.AbstractEditorComposite#initComponents()
     */
    @Override
    protected void initComponents() {
        createToolBar(SQL, this);
        // // Build the separator line
        createSqlText();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createSqlText".
     */
    private void createSqlText() {
        ColorManager colorManager = new ColorManager(DenguesCommonsPlugin.getDefault().getPreferenceStore());
        colorText = new ColorStyledText(this, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL, colorManager, EModeLanguage.SQL
                .getLanguageMode());
        Font font = new Font(this.getDisplay(), "courier", 10, SWT.NONE); //$NON-NLS-1$
        colorText.setFont(font);
        GridData gd = new GridData(GridData.FILL_BOTH);
        colorText.setLayoutData(gd);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createEditorProposal".
     */
    private void createEditorProposal() {
        try {
            // create KeyStroke use Ctrl+Space
            KeyStroke keyStroke = KeyStroke.getInstance("Ctrl+Space"); //$NON-NLS-1$
            IControlContentAdapter controlContentAdapter = new StyledTextContentAdapter();
            IContentProposalProvider contentProposalProvider = new SQLEditorProposalProvider(databaseContainer, EModeLanguage.SQL
                    .getLanguageMode());

            SQLEditorProposalAdapter contentProposalAdapter = new SQLEditorProposalAdapter(colorText, controlContentAdapter,
                    contentProposalProvider, keyStroke, new char[] { ' ', '.' });
            contentProposalAdapter.setPropagateKeys(true);
            contentProposalAdapter.setFilterStyle(ContentProposalAdapter.FILTER_CUMULATIVE);
            contentProposalAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
            contentProposalAdapter.setLabelProvider(new SQLEditorLabelProvider());
            contentProposalAdapter.setAutoActivationDelay(10);
            contentProposalAdapter.setPopupSize(new Point(300, 200));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Getter for colorText.
     * 
     * @return the colorText
     */
    public StyledText getColorText() {
        return this.colorText;
    }

    /**
     * Getter for databaseContainer.
     * 
     * @return the databaseContainer
     */
    public DatabaseContainer getDatabaseContainer() {
        return this.databaseContainer;
    }

    /**
     * Sets the databaseContainer.
     * 
     * @param databaseContainer the databaseContainer to set
     */
    public void setDatabaseContainer(DatabaseContainer databaseContainer) {
        this.databaseContainer = databaseContainer;
        createEditorProposal();
    }

    /**
     * Getter for dialog.
     * 
     * @return the dialog
     */
    @Override
    public DatabaseManagerDialog getDialog() {
        return this.dialog;
    }

    /**
     * Sets the dialog.
     * 
     * @param dialog the dialog to set
     */
    public void setDialog(DatabaseManagerDialog dialog) {
        this.dialog = dialog;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.warehouse.dbmanager.ui.AbstractEditorComposite#getSqlText()
     */
    @Override
    public String getSqlText() {
        return getColorText().getText();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.warehouse.dbmanager.ui.AbstractEditorComposite#addListener()
     */
    @Override
    protected void addListeners() {

    }

}
